﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using Word = Microsoft.Office.Interop.Word;

namespace greenwerkz.PlantMonitor
{
    public partial class frmContainer : Form
    {
        public class loginUser
        {
            public int growFacilityID { get; set; }
            public string userName { get; set; }
            public string userFName { get; set; }
            public string userLName { get; set; }
            public int umbracoMemberUserID { get; set; }
            public loginUser(greenwerkz.PlantMonitor.patients.GWpatient l_gwPatient)
            {
                userName = l_gwPatient.MemberUserName;
                userFName = l_gwPatient.PatientFirstName;
                userLName = l_gwPatient.PatientLastName;
                umbracoMemberUserID = l_gwPatient.MemberID;
                //hard-code for now
                growFacilityID = 1;
            }
            public loginUser(string l_userName, string l_password)
            {
                //hard-code user for now
                userName = "testuser";
                userFName = "Test";
                userLName = "User";
                umbracoMemberUserID = 666;
                growFacilityID = 1;
            }
        }
        public loginUser LoggedInUser { get; set; }
        public frmContainer()
        {
            InitializeComponent();
            //FrmMain mainForm = new FrmMain();
            frmLogin loginForm = new frmLogin();
            loginForm.MdiParent = this;
            loginForm.StartPosition = FormStartPosition.CenterScreen;
            loginForm.Show();
        }

        //events
        public void loginComplete(greenwerkz.PlantMonitor.patients.GWpatient l_user)
        {
            this.LoggedInUser=new loginUser(l_user);
            mnuFileLoginLogout.Text = "Logout";
            FrmMain l_frmMain = new FrmMain();
            l_frmMain.MdiParent = this;
            //l_frmMain.StartPosition = FormStartPosition.CenterScreen;
            l_frmMain.StartPosition = FormStartPosition.Manual;
            l_frmMain.Location = new Point((this.ClientSize.Width - l_frmMain.Width) / 2,
                                   (this.ClientSize.Height - l_frmMain.Height) / 2);
            
            
            //set user information

            l_frmMain.LoginUser(0, 1);

            //enable-disable file menu items
            enableFileActions(true);
        }

        private void enableFileActions(bool l_enableFlag)
        {
            //mnuFileLoginLogout.Text = "Logout";
            mnuFilePlantManagement.Enabled = l_enableFlag;
            mnuFilePrintLabels.Enabled = l_enableFlag;
            mnuFileUsers.Enabled = l_enableFlag;
            mnuFileReport.Enabled = l_enableFlag;
        }
        private void frmContainer_Load(object sender, EventArgs e)
        {

        }

        private void mnuFileNewMultiple_Click(object sender, EventArgs e)
        {
            if (((MenuItem)sender).Text.ToLower() == "logout")
            {
                //close child windows
                closeAllWindows();
                mnuFileLoginLogout.Text = "Login";
                MessageBox.Show("User is logged out");
            }
            else
            {
                frmLogin l_frmLogin = new frmLogin();
                l_frmLogin.MdiParent = this;
                l_frmLogin.StartPosition = FormStartPosition.CenterScreen;
                l_frmLogin.Show();
            }
            enableFileActions(false);
        }
        private void mnuWindowCascade_Click(object sender, System.EventArgs e)
        {
            this.LayoutMdi(MdiLayout.Cascade);
        }

        private void closeAllWindows()
        {
            foreach (Form l_childForm in MdiChildren)
            {
                l_childForm.Close();
            }
        }
        private void mnuHelpAbout_Click(object sender, System.EventArgs e)
        {
            frmAbout objfrmAbout = new frmAbout();
            objfrmAbout.ShowDialog();
        }

        private void mnuHelpAbout_Click_1(object sender, EventArgs e)
        {
            frmAbout objfrmAbout = new frmAbout();
            objfrmAbout.ShowDialog();
        }

        private void mnuFileExit_Click_1(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void mnuWindowTileVertical_Click_1(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.TileVertical);
        }

        private void mnuWindowTileHorizontal_Click_1(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.TileHorizontal);

        }

        private void mnuFileReport_Click(object sender, EventArgs e)
        {

        }

        private void mnuFileLabelItem1_Click(object sender, EventArgs e)
        {
            //open word document resource and plug in data source
            /*
            Word.Application wrdApp;
            Word.Document oDataDoc;
            Word.MailMerge wrdMailMerge;
            Object oMissing = System.Type.Missing;
            Object oFalse = false;
            Object oTrue = true;
            Object oName = Environment.CurrentDirectory + @"\reports\labels_3.docx";
            Object oFileName = Environment.CurrentDirectory + @"\saved.rtf";
            Object oFileFormat = Word.WdSaveFormat.wdFormatRTF;
            // Starting the Word Application
            wrdApp = new Word.ApplicationClass();
            wrdApp.Visible = true;
            wrdApp.WindowState = Word.WdWindowState.wdWindowStateMaximize;
            // Open the template
            oDataDoc = wrdApp.Documents.Add(ref oName, ref oFalse, ref oMissing, ref oMissing);
            oDataDoc.Activate();
            // Document 2 mailmerge format
            wrdMailMerge = oDataDoc.MailMerge;
            wrdMailMerge.HighlightMergeFields = false;
            wrdMailMerge.ViewMailMergeFieldCodes = 0; //this is for showing the data instead of fieldnames
            // The data part
            Object oConnection = "DSN=dsn_data"; //The DSN connection name
            Object oQuery = "SELECT * FROM some_table"; // The query to get data from the DSN
            Object oSubType = Word.WdMergeSubType.wdMergeSubTypeWord;
            //Open the data source and merge the fields
            wrdMailMerge.OpenDataSource("", ref oFileFormat, ref oMissing, ref oMissing, ref oTrue, ref oMissing,ref oMissing, ref oMissing, ref oFalse, ref oMissing, ref oMissing, ref oConnection, ref oQuery, ref oMissing, ref oMissing, ref oSubType);
            wrdMailMerge.SuppressBlankLines = true;
             
            // Save document
            oDataDoc.PrintPreview();
            oDataDoc.SaveAs(ref oFileName, ref oFileFormat, ref oMissing, ref oMissing, ref oTrue, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
            oDataDoc.Saved = true;
             
            // Unload objects from the memory
            wrdMailMerge = null;
            oDataDoc = null;
            wrdApp = null;
             * */
        }

    }
}
